package mac10.monitor;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class InfoMonitor extends JFrame
{
	private static final long serialVersionUID = -2229930351713220754L;

	public static InfoMonitor staticMonitor = new InfoMonitor();
    
    private JTextArea leftTextArea, rightTextArea;
    private JTextField[] agentsText, agentsPosition;

    public InfoMonitor ()
    {
        super("Informationsmonitor");
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int width = Math.min(d.width, 1000);
        int height = Math.min(d.height, 800);
        this.setSize(width, height);
        this.leftTextArea = new JTextArea(32, 40);
        this.rightTextArea = new JTextArea(32, 40);
        leftTextArea.setLineWrap(true);
        leftTextArea.setLineWrap(true);
        leftTextArea.setEditable(false);
        rightTextArea.setEditable(false);
        JScrollPane leftScrollPane = new JScrollPane(leftTextArea);
        JScrollPane rightScrollPane = new JScrollPane(rightTextArea);
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                leftScrollPane, rightScrollPane);
        splitPane.setDividerLocation(500);
        
        JPanel southPanel = initSouthPanel();
        
        JSplitPane mainPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
                splitPane, southPanel);
        this.add(mainPane);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    private JPanel initSouthPanel()
    {
        JPanel south = new JPanel();
        south.setLayout(new FlowLayout(FlowLayout.LEFT));
        agentsText = new JTextField[20];
        agentsPosition = new JTextField[20];
        for (int i = 0; i < agentsText.length; i++)
        {
            JPanel agentPanel = new JPanel();
            agentPanel.setLayout(new BoxLayout(agentPanel, BoxLayout.X_AXIS));
            JLabel agentLabel = new JLabel();
            if(i < 9)
            {
                agentLabel.setText("Agent   "+(i+1)+": ");
            }
            else
            {
                agentLabel.setText("Agent "+(i+1)+": ");
            }
            JLabel posLabel = new JLabel("Position: ");
            JLabel textLabel = new JLabel(" Text: ");
            agentsText[i] = new JTextField(20);
            agentsPosition[i] = new JTextField(6);
            agentPanel.add(agentLabel);
            agentPanel.add(posLabel);
            agentPanel.add(agentsPosition[i]);
            agentPanel.add(textLabel);
            agentPanel.add(agentsText[i]);
            south.add(agentPanel);
        }
        
        return south;
    }

    public void setLeftText(String text)
    {
        leftTextArea.setText(text);
    }
    
    public void setRightText(String text)
    {
        rightTextArea.setText(text);
    }
    
    public void addLineToLeftText(String line)
    {
        leftTextArea.append('\n'+line);
    }
    
    public void addLineToRightText(String line)
    {
        rightTextArea.append('\n'+line);
    }
    
    public void setAgentText(String text, String agentName)
    {
        Pattern p = Pattern.compile("\\d{1,2}");
        Matcher m = p.matcher(agentName);
        if ( m.find() )
        {
            int agent = Integer.parseInt(m.group());
            if(text == null)
                text = "null";
            
            agentsText[agent-1].setText(text);
        }
    }

    public void setAgentPosition(String position, String agentName)
    {
        Pattern p = Pattern.compile("\\d{1,2}");
        Matcher m = p.matcher(agentName);
        if ( m.find() )
        {
            int agent = Integer.parseInt(m.group());
            agentsPosition[agent-1].setText(position);
        }
    }
    
    public static void main(String[] args)
    {
        InfoMonitor i = new InfoMonitor();    
        i.isVisible();
    }
}
